package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.AppUser;
import com.ruoyi.system.domain.SysDevice;
import com.ruoyi.system.domain.SysDeviceCollectRecord;
import com.ruoyi.system.mapper.SysDeviceCollectRecordMapper;
import com.ruoyi.system.service.IAppUserService;
import com.ruoyi.system.service.ISysDeviceCollectRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author 10100
 */
@Service
public class SysDeviceCollectRecordServiceImpl implements ISysDeviceCollectRecordService {
    @Resource
    private SysDeviceCollectRecordMapper sysDeviceCollectRecordMapper;

    @Resource
    private IAppUserService appUserService;

    @Override
    public SysDeviceCollectRecord selectSysDeviceCollectRecordById(Long id) {
        return sysDeviceCollectRecordMapper.selectSysDeviceCollectRecordById(id);
    }

    @Override
    public List<SysDeviceCollectRecord> selectSysDeviceCollectRecordList(SysDeviceCollectRecord record) {
        return sysDeviceCollectRecordMapper.selectSysDeviceCollectRecordList(record);
    }

    @Override
    public int insertSysDeviceCollectRecord(SysDeviceCollectRecord record) {
        return sysDeviceCollectRecordMapper.insertSysDeviceCollectRecord(record);
    }

    @Override
    public int updateSysDeviceCollectRecord(SysDeviceCollectRecord record) {
        return sysDeviceCollectRecordMapper.updateSysDeviceCollectRecord(record);
    }

    @Override
    public int deleteSysDeviceCollectRecordByIds(Long[] ids) {
        return sysDeviceCollectRecordMapper.deleteSysDeviceCollectRecordByIds(ids);
    }

    @Override
    public void insertRecord(SysDevice device, String type, String value, String remark) {
        SysDeviceCollectRecord record = new SysDeviceCollectRecord();
        record.setDeviceIdentifier(device.getDeviceIdentifier());
        record.setDeviceId(device.getDeviceId());
        record.setRecordType(type);
        record.setRecordValue(value);
        record.setCollectTime(new Date());
        record.setRemark(remark);

        AppUser user = appUserService.selectAppUserById(device.getBindUserId());
        if (Objects.nonNull(user)) {
            record.setUserId(user.getId());
            record.setUserName(user.getLoginName());
        }
        sysDeviceCollectRecordMapper.insertSysDeviceCollectRecord(record);
    }
} 